<?php

class mapsearch{
public function index(){
$ytop = $latitude_top = floatval($GLOBALS['request']['latitude_top']);
$ybottom = $latitude_bottom = floatval($GLOBALS['request']['latitude_bottom']);
$xleft = $longitude_left = floatval($GLOBALS['request']['longitude_left']);
$xright = $longitude_right = floatval($GLOBALS['request']['longitude_right']);
$ypoint =  $m_latitude = doubleval($GLOBALS['request']['m_latitude']);
$xpoint = $m_longitude = doubleval($GLOBALS['request']['m_longitude']);
$type = intval($GLOBALS['request']['type']);
$pi = 3.14159265;
$r = 6378137;
$field_append = ", (ACOS(SIN(($ypoint * $pi) / 180 ) *SIN((ypoint * $pi) / 180 ) +COS(($ypoint * $pi) / 180 ) * COS((ypoint * $pi) / 180 ) *COS(($xpoint * $pi) / 180 - (xpoint * $pi) / 180 ) ) * $r) as distance ";
$condition = "  ypoint > $ybottom and ypoint < $ytop and xpoint > $xleft and xpoint < $xright ";
$limit = 10;
if($type==-1||$type==0)
{
$now = get_gmtime();
$sql = "select id,name,icon,xpoint,ypoint $field_append from ".DB_PREFIX.'youhui';
$where = ' where is_effect = 1 and begin_time<'.$now.' and (end_time = 0 or end_time > '.$now.') ';
$where.=' and '.$condition;
$sql.= $where;
$sql.=" limit $limit ";
$list = $GLOBALS['db']->getAll($sql);
$youhui_list = array();
foreach($list as $item){
$item['icon'] = get_abs_img_root($item['icon']);
$item['type'] = 0;
$item['distance'] = round($item['distance']);
$youhui_list[] = $item;
}
}
if($type==-1||$type==1)
{
$res = m_search_event_list($limit,0,0,$condition,' distance asc ',$field_append);
$event_list = array();
foreach($res['list'] as $item){
$item['icon'] = get_abs_img_root($item['icon']);
$item['type'] = 1;
$item['distance'] = round($item['distance']);
$event_list[] = $item;
}
}
if($type==-1||$type==2)
{
$res = m_get_deal_list($limit,0,0,array(DEAL_ONLINE),$condition,'distance asc',0,$field_append);
$tuan_list = array();
foreach($res['list'] as $item){
$item['icon'] = get_abs_img_root($item['icon']);
$item['type'] = 2;
$item['distance'] = round($item['distance']);
$tuan_list[] = $item;
}
}
if($type==-1||$type==3)
{
$res = m_search_youhui_list($limit,0,$condition,' distance asc ',0,$field_append);
$dianjin_list = array();
foreach($res['list'] as $item){
$item['icon'] = get_abs_img_root($item['icon']);
$item['type'] = 3;
$item['distance'] = round($item['distance']);
$dianjin_list[] = $item;
}
}
if($type==-1||$type==4)
{
$sql = "select id,name,address,preview as icon,xpoint,ypoint $field_append from   ".DB_PREFIX.'supplier_location  ';
$sql.=' where '.$condition;
$sql.=' order by distance asc limit '.$limit;
$list = $GLOBALS['db']->getAll($sql);
$merchant_list = array();
foreach($list as $item){
$item['icon'] = get_abs_img_root($item['icon']);
$item['type'] = 4;
$item['distance'] = round($item['distance']);
$merchant_list[] = $item;
}
}
if($type==-1)
{
$result_list = array_merge($youhui_list,$event_list,$tuan_list,$dianjin_list,$merchant_list);
}
elseif($type=0)
{
$result_list= $youhui_list;
}
elseif($type=1)
{
$result_list= $event_list;
}
elseif($type=2)
{
$result_list= $tuan_list;
}
elseif($type=3)
{
$result_list= $dianjin_list;
}
elseif($type=4)
{
$result_list= $merchant_list;
}
if($result_list)
$root['item'] = $result_list;
else
$root['item'] = array();
output($root);
}
}
?>